/**
 *
 */
package com.wedroid.r2d2.support.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wedroid.r2d2.contact.Contact;
import com.wedroid.r2d2.message.RawMessage;
import com.wedroid.r2d2.support.sync.SyncKey;

/**
 * @author lute
 *
 */
public class SyncResponse extends AbstractBaseResponse {

    private SyncKey syncKey;

    private List<RawMessage> rawMessages = new ArrayList<>();

    private List<Contact> modifiedContacts = new ArrayList<>();

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.support.response.AbstractBaseResponse#parseInternal(com.alibaba.fastjson.JSONObject)
     */
    @Override
    protected void parseInternal(JSONObject object) {
        // Parse sync key
        syncKey = object.getObject("SyncKey", SyncKey.class);

        // Parse messages
        JSONArray messageArray = object.getJSONArray("AddMsgList");
        for (int i = 0; i < messageArray.size(); i++) {
            rawMessages.add(messageArray.getObject(i, RawMessage.class));
        }

        int modContactCount = object.getIntValue("ModContactCount");
        if (modContactCount > 0) {
            JSONArray modContactList = object.getJSONArray("ModContactList");
            for (int i = 0; i < modContactList.size(); i++) {
                modifiedContacts.add(modContactList.getObject(i, Contact.class));
            }
        }
    }

    /**
     * @return the syncKey
     */
    public SyncKey getSyncKey() {
        return syncKey;
    }

    /**
     * @return the rawMessages
     */
    public List<RawMessage> getRawMessages() {
        return Collections.unmodifiableList(rawMessages);
    }

    /**
     * @return the modifiedContacts
     */
    public List<Contact> getModifiedContacts() {
        return Collections.unmodifiableList(modifiedContacts);
    }

}
